package com.dongdongshop.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    /*
        创建ShiroFilterFactoryBean
    */
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Autowired DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //设置会话管理
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        Map<String,String> filterMap = new LinkedHashMap<>();
        filterMap.put("/dongdongshop/login", "anon");
        filterMap.put("/content/listSlideshow", "anon");
        filterMap.put("/dongdongshop/index", "anon");
        filterMap.put("/user/toRegister", "anon");
        filterMap.put("/user/SendVerificationCode", "anon");
        filterMap.put("/user/insertUser", "anon");
        filterMap.put("/cart/setCart", "anon");
        filterMap.put("/evaluate/listEvaluate", "anon");
        filterMap.put("/pay/return_url", "anon");
        filterMap.put("/weather/getWeather", "anon");
        filterMap.put("/pay/notify_url", "anon");
        filterMap.put("/user/forgotPassword", "anon");
        filterMap.put("/cart/successShow", "anon");
        filterMap.put("/cart/getItemById", "anon");
        filterMap.put("/cart/cartShow", "anon");
        filterMap.put("/cart/getCart", "anon");
        filterMap.put("/user/login", "anon");
        filterMap.put("/css/**", "anon");
        filterMap.put("/img/**", "anon");
        filterMap.put("/js/**", "anon");
        filterMap.put("/data/**", "anon");
        filterMap.put("/fonts/**", "anon");
        filterMap.put("/plugins/**", "anon");
        filterMap.put("/auth/logout","logout");
        filterMap.put("/**","authc");
        shiroFilterFactoryBean.setLoginUrl("/dongdongshop/toLogin");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;

    }

    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Autowired UserRealm userRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean
    public UserRealm getUserRealm(){
        UserRealm userRealm = new UserRealm();
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher());
        return userRealm;
    }

    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("MD5");
        credentialsMatcher.setHashIterations(3);
        credentialsMatcher.setStoredCredentialsHexEncoded(true);
        return credentialsMatcher;
    }
}
